<?php
namespace Admin\Controller;
use Vendor\UploadFile;

class ShoppicController extends ComController
{
    public function index()
    {

    }

    public function fileinit(){
        $imgs = [];

        $guid = I('get.guid','');

        if (empty($guid)) {
            return $this->ajaxReturn($imgs);
        }

        $where = ['guid'=>$guid,'status'=>1];

        $res = M('Shop_imgs')->where($where)->select();

        if (empty($res)) {
          return $this->ajaxReturn($imgs);
        }

        foreach ($res as &$value) {
          $value['imgId'] = $value['id'];
          $value['path'] = $value['url'];
        }

        $imgs = $res;

        return $this->ajaxReturn($imgs);
    }

    public function fileupload(){
       $files = $_FILES;

       if (empty($files)) {
        $this->ajaxReturn(array('status'=>-1,'msg'=>'没有上传的文件'));
       }

       $act = 'shopSlider';
       $up = new uploadFile($files,$act,'',2000,0,1,2);
       $up->setSavename();
       $res = $up->run('file');

       if (!$res) {
            return $this->ajaxReturn(array('code'=>0,'msg'=>$up->errmsg()));
       }

       $info = $up->getInfo();

       $url = '/uploads/'.$act.'/'.date('Y-m-d').'/'.$info[0]['saveName'];

       $data = array(
            'guid' => I('get.guid'),
            'url' => $url,
            'status' => 0,
       );

       $addRes = M('Shop_imgs')->data($data)->add();

       var_dump($addRes);exit;

       if (!$addRes) {
            return $this->ajaxReturn(array('code'=>0,'添加失败'));
       }

       $res = array('code'=>1,'url'=>$url);
       $this->ajaxReturn($res);
   }

   public function fileupdate(){

   }

   public function filedel($id){
      $id = intval($id);

      if (empty($id)) {
        return $this->ajaxReturn(array('code'=>0,'msg'=>'参数错误-请选择要删除的照片'));
      }

      // $res = M('shop_imgs')->data(['status'=>0])->where(['id'=>$id])->save();
      $res = M('shop_imgs')->where(['id'=>$id])->delete();

      if (false === $res) {
        return $this->ajaxReturn(['code'=> 0 ,'msg'=>'删除失败']);
      }

      return $this->ajaxReturn(['code'=> 1 ,'msg'=>'删除成功']);
   }
}
